home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / text / faqs / unix-faq.faq.part4 < prev    next >
Encoding:
Internet Message Format  |  1994-05-16  |  25.9 KB

  1. Subject: Unix - Frequently Asked Questions (4/7) [Frequent posting]
  2. Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
  3. From: tmatimar@isgtec.com (Ted Timar)
  4. Date: 14 May 1994 10:18:51 GMT
  5.  
  6. Archive-name: unix-faq/faq/part4
  7. Version: $Id: part4,v 2.5 1994/04/28 19:25:03 tmatimar Exp $
  8.  
  9. These seven articles contain the answers to some Frequently Asked
  10. Questions often seen in comp.unix.questions and comp.unix.shell.
  11. Please don't ask these questions again, they've been answered plenty
  12. of times already - and please don't flame someone just because they may
  13. not have read this particular posting.  Thank you.
  14.  
  15. This collection of documents is Copyright (c) 1994, Ted Timar.
  16. All rights reserved.  Permission to distribute the collection is
  17. hereby granted providing that distribution is electronic, no money
  18. is involved, reasonable attempts are made to use the latest version
  19. and all credits and this copyright notice are maintained.
  20. Other requests for distribution will be considered.
  21.  
  22. All information here has been contributed with good intentions, but
  23. none of it is guaranteed either by the contributors or myself to be
  24. accurate.  The users of this information take all responsibility for
  25. any damage that may occur.
  26.  
  27. Many FAQs, including this one, are available on the archive site
  28. rtfm.mit.edu in the directory pub/usenet/news.answers.
  29. The name under which a FAQ is archived appears in the "Archive-Name:"
  30. line at the top of the article.  This FAQ is archived as
  31. "unix-faq/faq/part[1-7]".
  32.  
  33. These articles are divided approximately as follows:
  34.  
  35.       1.*) General questions.
  36.       2.*) Relatively basic questions, likely to be asked by beginners.
  37.       3.*) Intermediate questions.
  38.       4.*) Advanced questions, likely to be asked by people who thought
  39.        they already knew all of the answers.
  40.       5.*) Questions pertaining to the various shells, and the differences.
  41.       6.*) An overview of Unix variants.
  42.       7.*) An comparison of configuration management systems (RCS, SCCS).
  43.  
  44. This article includes answers to:
  45.  
  46.       4.1)  How do I read characters from a terminal without requiring the user
  47.               to hit RETURN?
  48.       4.2)  How do I check to see if there are characters to be read without
  49.               actually reading?
  50.       4.3)  How do I find the name of an open file?
  51.       4.4)  How can an executing program determine its own pathname?
  52.       4.5)  How do I use popen() to open a process for reading AND writing?
  53.       4.6)  How do I sleep() in a C program for less than one second?
  54.       4.7)  How can I get setuid shell scripts to work?
  55.       4.8)  How can I find out which user or process has a file open or is using
  56.             a particular file system (so that I can unmount it?)
  57.       4.9)  How do I keep track of people who are fingering me?
  58.       4.10) Is it possible to reconnect a process to a terminal after it has
  59.             been disconnected, e.g. after starting a program in the background
  60.             and logging out?
  61.       4.11) Is it possible to "spy" on a terminal, displaying the output
  62.             that's appearing on it on another terminal?
  63.  
  64. If you're looking for the answer to, say, question 4.5, and want to skip
  65. everything else, you can search ahead for the regular expression "^4.5)".
  66.  
  67. While these are all legitimate questions, they seem to crop up in
  68. comp.unix.questions or comp.unix.shell on an annual basis, usually
  69. followed by plenty of replies (only some of which are correct) and then
  70. a period of griping about how the same questions keep coming up.  You
  71. may also like to read the monthly article "Answers to Frequently Asked
  72. Questions" in the newsgroup "news.announce.newusers", which will tell
  73. you what "UNIX" stands for.
  74.  
  75. With the variety of Unix systems in the world, it's hard to guarantee
  76. that these answers will work everywhere.  Read your local manual pages
  77. before trying anything suggested here.  If you have suggestions or
  78. corrections for any of these answers, please send them to to
  79. tmatimar@isgtec.com.
  80.  
  81. ----------------------------------------------------------------------
  82.  
  83. Subject: How do I read characters ... without requiring the user to hit RETURN?
  84. Date: Thu Mar 18 17:16:55 EST 1993
  85.  
  86. 4.1)  How do I read characters from a terminal without requiring the user
  87.       to hit RETURN?
  88.  
  89.       Check out cbreak mode in BSD, ~ICANON mode in SysV.
  90.  
  91.       If you don't want to tackle setting the terminal parameters
  92.       yourself (using the "ioctl(2)" system call) you can let the stty
  93.       program do the work - but this is slow and inefficient, and you
  94.       should change the code to do it right some time:
  95.  
  96.       #include <stdio.h>
  97.       main()
  98.       {
  99.         int c;
  100.  
  101.         printf("Hit any character to continue\n");
  102.         /*
  103.          * ioctl() would be better here; only lazy
  104.          * programmers do it this way:
  105.          */
  106.         system("/bin/stty cbreak");        /* or "stty raw" */
  107.         c = getchar();
  108.         system("/bin/stty -cbreak");
  109.         printf("Thank you for typing %c.\n", c);
  110.  
  111.         exit(0);
  112.       }
  113.  
  114.       Several people have sent me various more correct solutions to
  115.       this problem.  I'm sorry that I'm not including any of them here,
  116.       because they really are beyond the scope of this list.
  117.  
  118.       You might like to check out the documentation for the "curses"
  119.       library of portable screen functions.  Often if you're interested
  120.       in single-character I/O like this, you're also interested in
  121.       doing some sort of screen display control, and the curses library
  122.       provides various portable routines for both functions.
  123.  
  124. ------------------------------
  125.  
  126. Subject: How do I check to see if there are characters to be read ... ?
  127. Date: Thu Mar 18 17:16:55 EST 1993
  128.  
  129. 4.2)  How do I check to see if there are characters to be read without
  130.       actually reading?
  131.  
  132.       Certain versions of UNIX provide ways to check whether characters
  133.       are currently available to be read from a file descriptor.  In
  134.       BSD, you can use select(2).  You can also use the FIONREAD ioctl
  135.       (see tty(4)), which returns the number of characters waiting to
  136.       be read, but only works on terminals, pipes and sockets.  In
  137.       System V Release 3, you can use poll(2), but that only works on
  138.       streams.  In Xenix - and therefore Unix SysV r3.2 and later - the
  139.       rdchk() system call reports whether a read() call on a given file
  140.       descriptor will block.
  141.  
  142.       There is no way to check whether characters are available to be
  143.       read from a FILE pointer.  (You could poke around inside stdio
  144.       data structures to see if the input buffer is nonempty, but that
  145.       wouldn't work since you'd have no way of knowing what will happen
  146.       the next time you try to fill the buffer.)
  147.  
  148.       Sometimes people ask this question with the intention of writing
  149.         if (characters available from fd)
  150.             read(fd, buf, sizeof buf);
  151.       in order to get the effect of a nonblocking read.  This is not
  152.       the best way to do this, because it is possible that characters
  153.       will be available when you test for availability, but will no
  154.       longer be available when you call read.  Instead, set the
  155.       O_NDELAY flag (which is also called FNDELAY under BSD) using the
  156.       F_SETFL option of fcntl(2).  Older systems (Version 7, 4.1 BSD)
  157.       don't have O_NDELAY; on these systems the closest you can get to
  158.       a nonblocking read is to use alarm(2) to time out the read.
  159.  
  160. ------------------------------
  161.  
  162. Subject: How do I find the name of an open file?
  163. Date: Thu Mar 18 17:16:55 EST 1993
  164.  
  165. 4.3)  How do I find the name of an open file?
  166.  
  167.       In general, this is too difficult.  The file descriptor may
  168.       be attached to a pipe or pty, in which case it has no name.
  169.       It may be attached to a file that has been removed.  It may
  170.       have multiple names, due to either hard or symbolic links.
  171.  
  172.       If you really need to do this, and be sure you think long
  173.       and hard about it and have decided that you have no choice,
  174.       you can use find with the -inum and possibly -xdev option,
  175.       or you can use ncheck, or you can recreate the functionality
  176.       of one of these within your program.  Just realize that
  177.       searching a 600 megabyte filesystem for a file that may not
  178.       even exist is going to take some time.
  179.  
  180. ------------------------------
  181.  
  182. Subject: How can an executing program determine its own pathname?
  183. Date: Thu Mar 18 17:16:55 EST 1993
  184.  
  185. 4.4)  How can an executing program determine its own pathname?
  186.  
  187.       Your program can look at argv[0]; if it begins with a "/", it is
  188.       probably the absolute pathname to your program, otherwise your
  189.       program can look at every directory named in the environment
  190.       variable PATH and try to find the first one that contains an
  191.       executable file whose name matches your program's argv[0] (which
  192.       by convention is the name of the file being executed).  By
  193.       concatenating that directory and the value of argv[0] you'd
  194.       probably have the right name.
  195.  
  196.       You can't really be sure though, since it is quite legal for one
  197.       program to exec() another with any value of argv[0] it desires.
  198.       It is merely a convention that new programs are exec'd with the
  199.       executable file name in argv[0].
  200.  
  201.       For instance, purely a hypothetical example:
  202.     
  203.     #include <stdio.h>
  204.     main()
  205.     {
  206.         execl("/usr/games/rogue", "vi Thesis", (char *)NULL);
  207.     }
  208.  
  209.       The executed program thinks its name (its argv[0] value) is
  210.       "vi Thesis".   (Certain other programs might also think that
  211.       the name of the program you're currently running is "vi Thesis",
  212.       but of course this is just a hypothetical example, don't
  213.       try it yourself :-)
  214.  
  215. ------------------------------
  216.  
  217. Subject: How do I use popen() to open a process for reading AND writing?
  218. Date: Thu Mar 18 17:16:55 EST 1993
  219.  
  220. 4.5)  How do I use popen() to open a process for reading AND writing?
  221.  
  222.       The problem with trying to pipe both input and output to an
  223.       arbitrary slave process is that deadlock can occur, if both
  224.       processes are waiting for not-yet-generated input at the same
  225.       time.  Deadlock can be avoided only by having BOTH sides follow a
  226.       strict deadlock-free protocol, but since that requires
  227.       cooperation from the processes it is inappropriate for a
  228.       popen()-like library function.
  229.  
  230.       The 'expect' distribution includes a library of functions that a
  231.       C programmer can call directly.  One of the functions does the
  232.       equivalent of a popen for both reading and writing.  It uses ptys
  233.       rather than pipes, and has no deadlock problem.  It's portable to
  234.       both BSD and SV.  See the next answer for more about 'expect'.
  235.  
  236. ------------------------------
  237.  
  238. Subject: How do I sleep() in a C program for less than one second?
  239. Date: Thu Mar 18 17:16:55 EST 1993
  240.  
  241. 4.6)  How do I sleep() in a C program for less than one second?
  242.  
  243.       The first thing you need to be aware of is that all you can
  244.       specify is a MINIMUM amount of delay; the actual delay will
  245.       depend on scheduling issues such as system load, and could be
  246.       arbitrarily large if you're unlucky.
  247.  
  248.       There is no standard library function that you can count on in
  249.       all environments for "napping" (the usual name for short
  250.       sleeps).  Some environments supply a "usleep(n)" function which
  251.       suspends execution for n microseconds.  If your environment
  252.       doesn't support usleep(), here are a couple of implementations
  253.       for BSD and System V environments.
  254.  
  255.       The following code is adapted from Doug Gwyn's System V emulation
  256.       support for 4BSD and exploits the 4BSD select() system call.
  257.       Doug originally called it 'nap()'; you probably want to call it
  258.       "usleep()";
  259.  
  260.       /*
  261.         usleep -- support routine for 4.2BSD system call emulations
  262.         last edit:    29-Oct-1984    D A Gwyn
  263.       */
  264.  
  265.       extern int    select();
  266.  
  267.       int
  268.       usleep( usec )                /* returns 0 if ok, else -1 */
  269.         long        usec;        /* delay in microseconds */
  270.         {
  271.         static struct            /* `timeval' */
  272.             {
  273.             long    tv_sec;        /* seconds */
  274.             long    tv_usec;    /* microsecs */
  275.             }    delay;        /* _select() timeout */
  276.  
  277.         delay.tv_sec = usec / 1000000L;
  278.         delay.tv_usec = usec % 1000000L;
  279.  
  280.         return select( 0, (long *)0, (long *)0, (long *)0, &delay );
  281.         }
  282.  
  283.       On System V you might do it this way:
  284.  
  285.       /*
  286.       subseconds sleeps for System V - or anything that has poll()
  287.       Don Libes, 4/1/1991
  288.  
  289.       The BSD analog to this function is defined in terms of
  290.       microseconds while poll() is defined in terms of milliseconds.
  291.       For compatibility, this function provides accuracy "over the long
  292.       run" by truncating actual requests to milliseconds and
  293.       accumulating microseconds across calls with the idea that you are
  294.       probably calling it in a tight loop, and that over the long run,
  295.       the error will even out.
  296.  
  297.       If you aren't calling it in a tight loop, then you almost
  298.       certainly aren't making microsecond-resolution requests anyway,
  299.       in which case you don't care about microseconds.  And if you did,
  300.       you wouldn't be using UNIX anyway because random system
  301.       indigestion (i.e., scheduling) can make mincemeat out of any
  302.       timing code.
  303.  
  304.       Returns 0 if successful timeout, -1 if unsuccessful.
  305.  
  306.       */
  307.  
  308.       #include <poll.h>
  309.  
  310.       int
  311.       usleep(usec)
  312.       unsigned int usec;        /* microseconds */
  313.       {
  314.         static subtotal = 0;    /* microseconds */
  315.         int msec;            /* milliseconds */
  316.  
  317.         /* 'foo' is only here because some versions of 5.3 have
  318.          * a bug where the first argument to poll() is checked
  319.          * for a valid memory address even if the second argument is 0.
  320.          */
  321.         struct pollfd foo;
  322.  
  323.         subtotal += usec;
  324.         /* if less then 1 msec request, do nothing but remember it */
  325.         if (subtotal < 1000) return(0);
  326.         msec = subtotal/1000;
  327.         subtotal = subtotal%1000;
  328.         return poll(&foo,(unsigned long)0,msec);
  329.       }
  330.  
  331.       Another possibility for nap()ing on System V, and probably other
  332.       non-BSD Unices is Jon Zeeff's s5nap package, posted to
  333.       comp.sources.misc, volume 4.  It does require a installing a
  334.       device driver, but works flawlessly once installed.  (Its
  335.       resolution is limited to the kernel HZ value, since it uses the
  336.       kernel delay() routine.)
  337.  
  338. ------------------------------
  339.  
  340. Subject: How can I get setuid shell scripts to work?
  341. Date: Thu Mar 18 17:16:55 EST 1993
  342.  
  343. 4.7)  How can I get setuid shell scripts to work?
  344.  
  345.       [ This is a long answer, but it's a complicated and frequently-asked
  346.         question.  Thanks to Maarten Litmaath for this answer, and
  347.         for the "indir" program mentioned below. ]
  348.  
  349.       Let us first assume you are on a UNIX variant (e.g. 4.3BSD or
  350.       SunOS) that knows about so-called `executable shell scripts'.
  351.       Such a script must start with a line like:
  352.  
  353.     #!/bin/sh
  354.  
  355.       The script is called `executable' because just like a real (binary)
  356.       executable it starts with a so-called `magic number' indicating
  357.       the type of the executable.  In our case this number is `#!' and
  358.       the OS takes the rest of the first line as the interpreter for
  359.       the script, possibly followed by 1 initial option like:
  360.  
  361.     #!/bin/sed -f
  362.  
  363.       Suppose this script is called `foo' and is found in /bin,
  364.       then if you type:
  365.  
  366.     foo arg1 arg2 arg3
  367.  
  368.       the OS will rearrange things as though you had typed:
  369.  
  370.     /bin/sed -f /bin/foo arg1 arg2 arg3
  371.  
  372.       There is one difference though: if the setuid permission bit for
  373.       `foo' is set, it will be honored in the first form of the
  374.       command; if you really type the second form, the OS will honor
  375.       the permission bits of /bin/sed, which is not setuid, of course.
  376.  
  377.       ----------
  378.  
  379.       OK, but what if my shell script does NOT start with such a `#!'
  380.       line or my OS does not know about it?
  381.  
  382.       Well, if the shell (or anybody else) tries to execute it, the OS
  383.       will return an error indication, as the file does not start with
  384.       a valid magic number.  Upon receiving this indication the shell
  385.       ASSUMES the file to be a shell script and gives it another try:
  386.  
  387.     /bin/sh shell_script arguments
  388.  
  389.       But we have already seen that a setuid bit on `shell_script' will
  390.       NOT be honored in this case!
  391.  
  392.       ----------
  393.  
  394.       Right, but what about the security risks of setuid shell scripts?
  395.  
  396.       Well, suppose the script is called `/etc/setuid_script', starting
  397.       with:
  398.  
  399.     #!/bin/sh
  400.     
  401.       Now let us see what happens if we issue the following commands:
  402.  
  403.     $ cd /tmp
  404.     $ ln /etc/setuid_script -i
  405.     $ PATH=.
  406.     $ -i
  407.  
  408.       We know the last command will be rearranged to:
  409.  
  410.     /bin/sh -i
  411.  
  412.       But this command will give us an interactive shell, setuid to the
  413.       owner of the script!
  414.       Fortunately this security hole can easily be closed by making the
  415.       first line:
  416.  
  417.     #!/bin/sh -
  418.  
  419.       The `-' signals the end of the option list: the next argument `-i'
  420.       will be taken as the name of the file to read commands from, just
  421.       like it should!
  422.  
  423.       ---------
  424.  
  425.       There are more serious problems though:
  426.  
  427.     $ cd /tmp
  428.     $ ln /etc/setuid_script temp
  429.     $ nice -20 temp &
  430.     $ mv my_script temp
  431.  
  432.       The third command will be rearranged to:
  433.  
  434.     nice -20 /bin/sh - temp
  435.  
  436.       As this command runs so slowly, the fourth command might be able
  437.       to replace the original `temp' with `my_script' BEFORE `temp' is
  438.       opened by the shell!  There are 4 ways to fix this security hole:
  439.  
  440.     1)  let the OS start setuid scripts in a different, secure way
  441.         - System V R4 and 4.4BSD use the /dev/fd driver to pass the
  442.         interpreter a file descriptor for the script
  443.  
  444.     2)  let the script be interpreted indirectly, through a frontend
  445.         that makes sure everything is all right before starting the
  446.         real interpreter - if you use the `indir' program from
  447.         comp.sources.unix the setuid script will look like this:
  448.  
  449.         #!/bin/indir -u
  450.         #?/bin/sh /etc/setuid_script
  451.  
  452.     3)  make a `binary wrapper': a real executable that is setuid and
  453.         whose only task is to execute the interpreter with the name of
  454.         the script as an argument
  455.  
  456.     4)  make a general `setuid script server' that tries to locate the
  457.         requested `service' in a database of valid scripts and upon
  458.         success will start the right interpreter with the right
  459.         arguments.
  460.  
  461.       ---------
  462.  
  463.       Now that we have made sure the right file gets interpreted, are
  464.       there any risks left?
  465.  
  466.       Certainly!  For shell scripts you must not forget to set the PATH
  467.       variable to a safe path explicitly.  Can you figure out why?
  468.       Also there is the IFS variable that might cause trouble if not
  469.       set properly.  Other environment variables might turn out to
  470.       compromise security as well, e.g. SHELL...  Furthermore you must
  471.       make sure the commands in the script do not allow interactive
  472.       shell escapes!  Then there is the umask which may have been set
  473.       to something strange...
  474.  
  475.       Etcetera.  You should realise that a setuid script `inherits' all
  476.       the bugs and security risks of the commands that it calls!
  477.  
  478.       All in all we get the impression setuid shell scripts are quite a
  479.       risky business!  You may be better off writing a C program instead!
  480.  
  481. ------------------------------
  482.  
  483. Subject: How can I find out which user or process has a file open ... ?
  484. Date: Thu Mar 18 17:16:55 EST 1993
  485.  
  486. 4.8)  How can I find out which user or process has a file open or is using
  487.       a particular file system (so that I can unmount it?)
  488.  
  489.       Use fuser (system V), fstat (BSD), ofiles (public domain) or
  490.       pff (public domain).  These programs will tell you various things
  491.       about processes using particular files.
  492.  
  493.       A port of the 4.3 BSD fstat to Dynix, SunOS and Ultrix
  494.       can be found in archives of comp.sources.unix, volume 18.
  495.  
  496.       pff is part of the kstuff package, and works on quite a few systems.
  497.       Instructions for obtaining kstuff are provided in question 3.10.
  498.  
  499. ------------------------------
  500.  
  501. Subject: How do I keep track of people who are fingering me?
  502. From: jik@rtfm.MIT.EDU (Jonathan I. Kamens)
  503. From: malenovi@plains.NoDak.edu (Nikola Malenovic)
  504. Date: Mon, 23 Nov 1992 16:01:45 -0600
  505.  
  506. 4.9)  How do I keep track of people who are fingering me?
  507.  
  508.       Generally, you can't find out the userid of someone who is
  509.       fingering you from a remote machine.  You may be able to
  510.       find out which machine the remote request is coming from.
  511.       One possibility, if your system supports it and assuming
  512.       the finger daemon doesn't object, is to make your .plan file a
  513.       "named pipe" instead of a plain file.  (Use 'mknod' to do this.)
  514.  
  515.       You can then start up a program that will open your .plan file
  516.       for writing; the open will block until some other process (namely
  517.       fingerd) opens the .plan for reading.  Now you can whatever you
  518.       want through this pipe, which lets you show different .plan
  519.       information every time someone fingers you.
  520.  
  521.       Of course, this may not work at all if your system doesn't
  522.       support named pipes or if your local fingerd insists
  523.       on having plain .plan files.
  524.  
  525.       Your program can also take the opportunity to look at the output
  526.       of "netstat" and spot where an incoming finger connection is
  527.       coming from, but this won't get you the remote user.
  528.  
  529.       Getting the remote userid would require that the remote site be
  530.       running an identity service such as RFC 931.  There are now three
  531.       RFC 931 implementations for popular BSD machines, and several
  532.       applications (such as the wuarchive ftpd) supporting the server.
  533.       For more information join the rfc931-users mailing list,
  534.       rfc931-users-request@kramden.acf.nyu.edu.
  535.  
  536.       There are three caveats relating to this answer.  The first is
  537.       that many NFS systems won't recognize the named pipe correctly.
  538.       This means that trying to read the pipe on another machine will
  539.       either block until it times out, or see it as a zero-length file,
  540.       and never print it.
  541.  
  542.       The second problem is that on many systems, fingerd checks that
  543.       the .plan file contains data (and is readable) before trying to
  544.       read it.  This will cause remote fingers to miss your .plan file
  545.       entirely.
  546.  
  547.       The third problem is that a system that supports named pipes
  548.       usually has a fixed number of named pipes available on the
  549.       system at any given time - check the kernel config file and
  550.       FIFOCNT option.  If the number of pipes on the system exceeds the
  551.       FIFOCNT value, the system blocks new pipes until somebody frees
  552.       the resources.  The reason for this is that buffers are allocated
  553.       in a non-paged memory.
  554.  
  555. ------------------------------
  556.  
  557. Subject: Is it possible to reconnect a process to a terminal ... ?
  558. Date: Thu Mar 18 17:16:55 EST 1993
  559.  
  560. 4.10) Is it possible to reconnect a process to a terminal after it has
  561.       been disconnected, e.g. after starting a program in the background
  562.       and logging out?
  563.  
  564.       Most variants of Unix do not support "detaching" and "attaching"
  565.       processes, as operating systems such as VMS and Multics support.
  566.       However, there are two freely redistributable packages which can
  567.       be used to start processes in such a way that they can be later
  568.       reattached to a terminal.
  569.  
  570.       The first is "screen," which is described in the
  571.       comp.sources.unix archives as "Screen, multiple windows on a CRT"
  572.       (see the "screen-3.2" package in comp.sources.misc, volume 28.)
  573.       This package will run on at least BSD, System V r3.2 and SCO UNIX.
  574.  
  575.       The second is "pty," which is described in the comp.sources.unix
  576.       archives as a package to "Run a program under a pty session" (see
  577.       "pty" in volume 23).  pty is designed for use under BSD-like
  578.       system only.
  579.  
  580.       Neither of these packages is retroactive, i.e. you must have
  581.       started a process under screen or pty in order to be able to
  582.       detach and reattach it.
  583.  
  584. ------------------------------
  585.  
  586. Subject: Is it possible to "spy" on a terminal ... ?
  587. Date: Thu Mar 18 17:16:55 EST 1993
  588.  
  589. 4.11) Is it possible to "spy" on a terminal, displaying the output
  590.       that's appearing on it on another terminal?
  591.  
  592.       There are a few different ways you can do this, although none
  593.       of them is perfect:
  594.  
  595.       * kibitz allows two (or more) people to interact with a shell
  596.         (or any arbitary program).  Uses include:
  597.  
  598.     - watching or aiding another person's terminal session;
  599.     - recording a conversation while retaining the ability to
  600.       scroll backwards, save the conversation, or even edit it
  601.       while in progress;
  602.     - teaming up on games, document editing, or other cooperative
  603.       tasks where each person has strengths and weakness that
  604.       complement one another.
  605.  
  606.         kibitz comes as part of the expect distribution.  See question 3.9.
  607.  
  608.         kibitz requires permission from the person to be spyed upon.  To
  609.         spy without permission requires less pleasant approaches:
  610.  
  611.       * You can write a program that rummages through Kernel structures
  612.         and watches the output buffer for the terminal in question,
  613.         displaying characters as they are output.  This, obviously, is
  614.         not something that should be attempted by anyone who does not
  615.         have experience working with the Unix kernel.  Furthermore,
  616.         whatever method you come up with will probably be quite
  617.         non-portable.
  618.  
  619.       * If you want to do this to a particular hard-wired terminal all
  620.         the time (e.g. if you want operators to be able to check the
  621.         console terminal of a machine from other machines), you can
  622.         actually splice a monitor into the cable for the terminal.  For
  623.         example, plug the monitor output into another machine's serial
  624.         port, and run a program on that port that stores its input
  625.         somewhere and then transmits it out *another* port, this one
  626.         really going to the physical terminal.  If you do this, you have
  627.         to make sure that any output from the terminal is transmitted
  628.         back over the wire, although if you splice only into the
  629.         computer->terminal wires, this isn't much of a problem.  This is
  630.         not something that should be attempted by anyone who is not very
  631.         familiar with terminal wiring and such.
  632.  
  633. ------------------------------
  634.  
  635. End of unix/faq Digest part 4 of 7
  636. **********************************
  637.  
  638. -- 
  639. Ted Timar - tmatimar@isgtec.com
  640. ISG Technologies Inc., 6509 Airport Road, Mississauga, Ontario, Canada L4V 1S7
  641.  
  642.